#include "../Base/stdafx.h"
#include "../Base/DxDevice.h"
#include "SpriteRender.h"
#include "Sprite.h"

SpriteRender::SpriteRender(): m_RenderPtr(0), m_BlendStatePtr(0)
{
	Init();
}

SpriteRender::~SpriteRender()
{
	for(int t = 0; t < (int)m_Sprites.size(); ++t)
		if(m_Sprites.at(t)) delete m_Sprites.at(t);

	if(m_RenderPtr) m_RenderPtr->Release();
}

HRESULT SpriteRender::Init()
{
	HRESULT hr = D3DX10CreateSprite(DXDevice::GetSingleton()->GetDevice(), 512, &m_RenderPtr);
	return hr;
}

void SpriteRender::Draw()
{
	m_RenderPtr->SetProjectionTransform(&DXDEVICE->GetSpriteViewProj());

	DXDEVICE->EnableBlend();
	//DXDevice::GetSingleton()->GetDevice()->OMSetBlendState(NULL, 0, 0xffffffff);
	m_RenderPtr->Begin(D3DX10_SPRITE_SORT_DEPTH_BACK_TO_FRONT);
	for(int t = 0; t < (int)m_Sprites.size(); ++t)
		m_RenderPtr->DrawSpritesBuffered(&m_Sprites.at(t)->GetSprite(), 1);

	m_RenderPtr->Flush();
	m_RenderPtr->End();
	DXDEVICE->DisableBlend();
}

void SpriteRender::AddSprite(Sprite *s)
{
	m_Sprites.push_back(s);
}

void SpriteRender::Tick(float dTime)
{
	for(int t = 0; t < (int)m_Sprites.size(); ++t)
		m_Sprites.at(t)->Tick(dTime);
}